<?php

namespace Admin\Controller;

use Think\Controller;

class PublicController extends Controller {

    /**
     * 系统后台 - 管理员登陆
     */
    public function login() {
        if (IS_POST) {
            $username = I('username');
            $password = I('password');
            $code = I('code');
            if ($username && $password) {
                //验证码校验
                if (C('need_verify')) {
                    if ($this->check_verify($code)) {
                        $this->error('登录失败！验证码失效！', U('Admin/Public/login'));
                    }
                }
                //echo pwdbysalt('admin');die;
                //加密密码                
                $data['username'] = $username;
                $data['password'] = pwdbysalt($password);
                $res = M('admin')->where($data)->find();
                if (empty($res)) {
                    $this->error('登陆失败！账号密码不匹配！');
                } else {
                    $this->authorload($res);
                    $this->success('登陆成功！', U('Admin/Index/index'));
                }
            } else {
                $this->error('登陆失败！账号密码输入有误！');
            }
        } else {
            $this->display('login');
        }
    }
    
    public function logout(){
        session('user',null);
        $this->display();
    }
    /**
     * 修改密码
     */
    public function pwd_edit() {
        if (IS_POST) {
            $oldpwd = I('oldpwd');
            $newpwd = I('newpwd');
            $newpwd2 = I('newpwd2');
            $admin_id = I('admin_id');

            if (empty($oldpwd) || empty($newpwd) || empty($newpwd2)) {
                $this->error('参数错误！');
            } else {
                if ($newpwd != $newpwd2) {
                    $this->error('密码与确认密码不一致！');
                } else {
                    $re = M('admin')->where('aid = ' . $admin_id . " and password = '" . pwdbysalt($oldpwd) . "'")->find();
                    if ($re) {
                        $map['password'] = pwdbysalt($newpwd);
                        $res = M('admin')->where('aid = ' . $admin_id)->save($map);
                        if ($res) {
                            $this->success('修改成功！', U('Admin/Public/login'), 3);
                        } else {
                            $this->error('修改失败！');
                        }
                    } else {
                        $this->error('原密码错误！');
                    }
                }
            }
        } else {
            $sess = session('user');
            $this->assign('aid', $sess['aid']);
            $this->display();
        }
    }

    /**
     * 管理员个人中心
     */
    public function u_center() {
        $admin = session('user');
        $admin_id = $admin['aid'];
        $re = M('admin')->alias('a')
                ->field('a.*')
                ->where('aid = ' . $admin_id)
                ->find();
        $this->assign('info', $re);
        $this->display();
    }

    /**
     * 修改个人信息
     */
    public function u_edit() {
        if (IS_POST) {
            $map['username'] = $username = I('username');
            $map['nickname'] = $nickname = I('nickname');
            $http = $_SERVER['HTTP_HOST'];
            if (substr($http, 0, 4) == 'http') {
                $map['headimg'] = $http . img_headimg($_FILES['headimg']);
            } else {
                $map['headimg'] = "http://" . $http . img_headimg($_FILES['headimg']);
            }
            $admin_id = I('admin_id');
            $res = M('admin')->where('aid = ' . $admin_id)->save($map);
            if ($res) {
                $_SESSION['user']['headimg'] = $map['headimg'];
                $this->success('修改成功！', U('u_center'), 3);
            } else {
                $this->error('修改失败！');
            }
        } else {
            $admin_id = I('admin_id');
            $info = M('admin')->where('aid = ' . $admin_id)->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 生成验证码
     */
    public function verify() {
        $Verify = new \Think\Verify(C('verify_config'));
        $Verify->entry();
    }

    /**
     * 检测输入的验证码是否正确，$code为用户输入的验证码字符串
     * @param type $code
     * @param type $id  一个页面生成多个验证码时，需要
     * @return type
     */
    private function check_verify($code, $id = '') {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    /**
     * 用户信息载入session
     * @param type $res
     */
    private function authorload($res) {
        if (!empty($res)) {
            session('user', $res);
            $data = array(
                'last_login_time' => time(),
                'last_login_ip' => get_client_ip()
            );
            M('admin')->where('aid = ' . $res['aid'])->setField($data);
        }
    }

    public function ajaxupload() {
        if (IS_POST) {
            if (empty($_FILES)) {
                exit(json_encode(array('status' => 0, 'msg' => '上传文件为空！')));
            } else {
                $file_name = array_keys($_FILES);
                $file_name = $file_name[0];
                
                if (null == C('proof')) {
                    $upload->maxSize = 2097152;
                    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                    $upload->savePath = './Public/uploads/proof/'; 
                } else {
                    $upload = new \Think\Upload(C('proof'));                    
                }
                $upload->saveName = $file_name . time() . '_' . mt_rand();
                $info = $upload->upload();
                if (!$info) {
                    exit(json_encode(array('status' => 0, 'msg' => $upload->getError())));
                    die;
                } else {
                    exit(json_encode(array('status' => 1, 'file_name' => IMG_SERVER.$info[$file_name]['savepath'] . $info[$file_name]['savename'])));
                }
            }
        } else {
            exit('不允许直接访问！');
        }
    }

}
